/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.util.reflection;

import java.util.logging.Logger;

/**
 *
 * @author selkhateeb
 */
public class Conversion
{

    public static <T> T Cast(String value, Class objectType)
    {
        try
        {
            Logger.getLogger(Conversion.class.getName()).info("Converting [" + value + "] into type" + objectType);
            Logger.getLogger(Conversion.class.getName()).info(objectType.getName());
            Logger.getLogger(Conversion.class.getName()).info(Integer.class.getName());
            if (objectType.equals(boolean.class))
            {
                return (T)Boolean.valueOf(value);
            }
            else if (objectType.equals(byte.class))
            {
                return (T)Byte.valueOf(value);
            }
            else if (objectType.equals(short.class))
            {
                return (T)Short.valueOf(value);
            }
            else if (objectType.equals(Integer.class))
            {
                Logger.getLogger(Conversion.class.getName()).info("in " + Integer.class.getName());
                return (T)Integer.valueOf(value);
            }
            else if (objectType.equals(Long.class))
            {
                Logger.getLogger(Conversion.class.getName()).info("in " + Long.class.getName());
                return (T)Long.valueOf(value);
            }
            else if (objectType.equals(float.class))
            {
                return (T)Float.valueOf(value);
            }
            else if (objectType.equals(double.class))
            {
                return (T)Double.valueOf(value);
            }
            else if (objectType.equals(char.class))
            {
                return (T)new Character(value.charAt(0));
            }
            else
            {
                return (T)objectType.cast(value);
            }
        }
        catch (Exception ex)
        {
            Logger.getLogger(Conversion.class.getName()).severe(ex.getMessage());
        }
        return (T)value;
    }
}
